package com.example.questionnaires.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnaires.common.Result;
import com.example.questionnaires.entity.Message;
import com.example.questionnaires.service.MessageService;
import com.example.questionnaires.util.UserHolder;
import com.example.questionnaires.common.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/message")
public class MessageController {
    private static final Logger log = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    @GetMapping("/list")
    public Result<PageResult<Message>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            return messageService.list(page, size);
        } catch (Exception e) {
            log.error("获取留言列表失败", e);
            return Result.error("获取留言列表失败");
        }
    }

    @PostMapping
    public Result<?> save(@RequestBody Message message) {
        message.setUserId(UserHolder.getUser().getId());
        messageService.save(message);
        return Result.success("发表成功");
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        messageService.removeById(id);
        return Result.success("删除成功");
    }
} 